/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.monkey;

import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInspection.*;
import com.intellij.psi.*;

public class ReferencedFieldInspection extends LocalInspectionTool {

    protected PsiElement getContainingElement(PsiElement element, Class requiredClass) {
        while (element != null && !(requiredClass.isAssignableFrom(element.getClass()))) {
            element = element.getParent();
        }
        return element;
    }

    public String getGroupDisplayName() {
        return "PurpleMonkeyDishwasher";
    }

    public String getDisplayName() {
        return "Check Referenced Fields";
    }

    public String getShortName() {
        return "CheckReferencedField";
    }

    public boolean isEnabledByDefault() {
        return true;
    }

    public HighlightDisplayLevel getDefaultLevel() {
        return HighlightDisplayLevel.ERROR;
    }

    public ProblemDescriptor[] checkField(PsiField field, InspectionManager manager, boolean isOnTheFly) {
        PsiExpression initializer = field.getInitializer();
        if (initializer == null) {
            if (checkIsTest(field.getContainingFile())) {
                PsiReference[] references = field.getManager().getSearchHelper().findReferences(field, field.getUseScope(), true);
                for (int i = 0; i < references.length; i++) {
                    PsiReference reference = references[i];
                    if (reference instanceof PsiReferenceExpression) {
                        PsiReferenceExpression psiReferenceExpression = (PsiReferenceExpression) reference;
                        PsiField psiField = (PsiField) getContainingElement(psiReferenceExpression, PsiField.class);
                        if (psiField != null) {
                            return new ProblemDescriptor[]{manager.createProblemDescriptor(field, "This field needs an initializer as it is referred to by other fields.",
                                    (LocalQuickFix) null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING)};

                        }

                    }
                }
            }
        }
        return null;
    }


    private boolean checkIsTest(PsiFile containingFile) {
        return containingFile.getName().endsWith("Test.java");
//        ProjectRootManager rootManager = ProjectRootManager
//                .getInstance(containingFile.getProject());
//        ProjectFileIndex fileIndex = rootManager.getFileIndex();
//
//        return fileIndex.isInTestSourceContent(containingFile.getVirtualFile());

    }
}
